﻿using System;
using X.Web;
using X.Entity;
using System.Web.UI.WebControls;

namespace X.WebSite.Admin.Product
{
    public partial class CategoryDetail : AdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlParentCategory.DataSource = Bll.ProductCategory.GetList();
                ddlParentCategory.DataTextField = "CategoryName";
                ddlParentCategory.DataValueField = "CategoryId";
                ddlParentCategory.DataBind();

                ddlParentCategory.Items.Insert(0, new ListItem("一级类别", "0"));

                if (!string.IsNullOrEmpty(Request.QueryString["CategoryId"]))
                {
                    ProductCategoryInfo categoryInfo = Bll.ProductCategory.GetCategory(int.Parse(Request.QueryString["CategoryId"]));

                    txtCategoryName.Text = X.Common.StringUtils.HtmlDecode(categoryInfo.CategoryName);
                    ddlParentCategory.SelectedValue = categoryInfo.ParentCategoryId.ToString();
                    chkShowOnMenu.Checked = categoryInfo.ShowOnMenu;
                    txtOrderNum.Text = categoryInfo.OrderNum.ToString();
                    chkIsShow.Checked = categoryInfo.IsShow;
                    txtTitle.Text = X.Common.StringUtils.HtmlDecode(categoryInfo.Title);
                    txtKeyword.Text = X.Common.StringUtils.HtmlDecode(categoryInfo.Keyword);
                    txtDescript.Text = X.Common.StringUtils.HtmlDecode(categoryInfo.Descript);
                    //txtLongWord.Text = categoryInfo.LongWord;
                    ckeCategoryDescript.Text = categoryInfo.CategoryDescript;
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            ProductCategoryInfo categoryInfo;

            if (!string.IsNullOrEmpty(Request.QueryString["CategoryId"]))
            {
                categoryInfo = Bll.ProductCategory.GetCategory(int.Parse(Request.QueryString["CategoryId"]));
            }
            else
            {
                categoryInfo = new ProductCategoryInfo();
            }

            categoryInfo.CategoryName = X.Common.StringUtils.HtmlEncode(txtCategoryName.Text);
            categoryInfo.ParentCategoryId = int.Parse(ddlParentCategory.SelectedValue);
            categoryInfo.ShowOnMenu = chkShowOnMenu.Checked;
            categoryInfo.OrderNum = int.Parse(txtOrderNum.Text);
            categoryInfo.IsShow = chkIsShow.Checked;
            categoryInfo.Title = X.Common.StringUtils.HtmlEncode(txtTitle.Text);
            categoryInfo.Keyword = X.Common.StringUtils.HtmlEncode(txtKeyword.Text);
            categoryInfo.Descript = X.Common.StringUtils.HtmlEncode(txtDescript.Text);
            //categoryInfo.LongWord = txtLongWord.Text;
            categoryInfo.CategoryDescript = ckeCategoryDescript.Text;

            if (!string.IsNullOrEmpty(Request.QueryString["CategoryId"]))
            {
                Bll.ProductCategory.Update(categoryInfo);
            }
            else
            {
                Bll.ProductCategory.Create(categoryInfo);
            }

            Response.Redirect("CategoryList.aspx");
        }
    }
}